<?php

require_once 'Base.php';

class PlaceOrderJsapi extends Base{

    //下单
    public function pay(){
        if($_POST){
            $requestParams=$_POST;
            $requestParams['sign']=$this->generateSign($requestParams,'MD5');
            $url="https://api.mch.weixin.qq.com/pay/unifiedorder";

            $response=$this->curlRequest($url,$this->arrayToXml($requestParams));
            if($response['return_code'] != 'SUCCESS'){
                exit($response['return_msg']);
            }
            if($response['result_code'] != 'SUCCESS'){
                exit($response['err_code_des']);
            }
            $this->checkSign($response);

            return $this->generateQRCode($response);
        }
    }

    //生成二维码
    private function generateQRCode($response){
        ob_start();
        ?>
        <!doctype html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport"
                  content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
            <meta http-equiv="X-UA-Compatible" content="ie=edge">
            <title>微信扫码支付</title>
        </head>
        <style>
            html,body{
                height:100%;
                width:100%;
                overflow:hidden;
                margin:0;
                padding:0;
            }
            body{
                background-color: #444444;
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
            }
        </style>
        <body>
            <div id="qrcode"></div>
        </body>
        <script type="text/javascript" src="../assets/js/qrcode.js"></script>
        <script type="text/javascript">
            var qrcode = new QRCode(document.getElementById("qrcode"), {
                width : 220,
                height : 220
            });
            qrcode.makeCode("<?php echo $response['code_url'];?>");
        </script>
        </html>
        <?php
        return ob_get_clean();
    }
}

echo (new PlaceOrderJsapi())->pay();